<?php

session_start();

$caidan = array();
require_once 'caidan.php';

$la_array = array('不辣', '微辣', '中辣', '特辣');


include './source/class/class_core.php';
$date_time = strtotime(date('Y-m-d'));
$source = DB::query("SELECT o.*, u.money FROM `om_order` as o inner join `om_user` as u on o.order_index = '$date_time' and o.user_id = u.id");
while ($row = DB::fetch($source)){
	$data[$row['name']] = unserialize($row['order_info']);
	$data[$row['name']]['money'] = $row['money'];
	$data[$row['name']]['id'] = $row['id'];
	$data[$row['name']]['user_id'] = $row['user_id'];
}

if (empty($data)){
	header("Content-type: text/html; charset=utf-8");
	die('今天还没有人订餐！');
}


?>
<!doctype html>
<html>
<head>
	<meta charset="utf-8">
	<title>总单详情</title>
	<link rel="stylesheet" href="images/global.min.css">
</head>
<script src="js/jquery.js" language="javascript" type="text/javascript"></script>
<script>
function account_order(user_id, user_name, money){
	$.ajax({
	   type: "POST",
	   url: "user_center.php",
	   data: "action=account&user_id=" + user_id + "&user_name=" + user_name + "&money=" + money,
	   success: function(msg){
		 if(msg == '1'){
			$('#btn_submit_' + user_id).val('结算成功');
		 }
	   }
	});
}
</script>
<body id="history">
    <div id="header">
        <strong class="fl">你好：<?=$_SESSION['name'] ?></strong>
        你的订单已被高亮，如有错请按右边链接重新下单！
        <div class="fr">
        <a href="index.php">重新下单</a>
        </div>
    </div>
	<table>
<?php
foreach ($data as $username => $val){
	$class = $username == $_SESSION['name'] ? ' class="highlight"':'';
	echo "<tr".$class.">";

   //echo "<tr>";

	if (array_sum($val['num']) > 0){
		echo '<td>' . $username . '</td>';
		echo "<td>";
		$lit_total = 0;
		foreach ($val['num'] as $k => $v){
			if ($v > 0){
				$price = $v * $caidan[$k]['price'];
				$lit_total += $price;
				$total += $price;
				$allv += $v;
				echo '<span>'.$caidan[$k]['name'].'</span>' . $v . "份 [" . $la_array[intval($val['la'][$k])] . ']<br />';

			}

		}
		echo "</td>";
		echo '<td>'. '备注：' . $val['remark'] . '</td>';
		echo '<td>'.'小计：' . $lit_total . '元' . '</td>';
		echo '<td>用户余额：'.$val['money'].'</td>';
		
		
		
		if($_SESSION['name'] == 'MM' & $val['money'] < 0){
			echo '<td>';
			echo '<input type="button" id="btn_submit_'.$val['user_id'].'" value="结算" onclick="this.disabled = true;account_order('.$val['user_id'].', \''.$username.'\', '.$val['money'].');" />';
			echo '</td>';
		}
		
		
	}
	echo "</tr>";
?>

<?php
}

echo '<tr><td></td>'.'<td>总份数：'. $allv .'</td>'.'<td></td>'.'<td>总计：' . $total . '元</td>';
echo '<tr><td colspan="3">多玩游戏网</td>'.'<td>八折总价：'.sprintf('%.2f', $total * 0.8).'元</td>';
echo "</table>";


if (date('Y-m-d') == date("Y-m-d", filectime('info.txt'))){
	echo file_get_contents('info.txt');
}



?>